package com.example.saveqqpassword;

import android.content.Context;
import android.util.Log;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Type;
import java.util.Map;

public class SaveInfobyJson {
    private static String unm = "username";
    private static String pwd = "password";
    private static String fileName = "QQInfo";

    public  static boolean saveQQInfo(Context context, String username , String password ,OutputStream out) throws JSONException {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put(unm,username);
        jsonObject.put(pwd,password);
        Log.v("life","saveQQInfo");
        try {
            out.write(jsonObject.toString().getBytes());//写入的字符串}
            out.flush();
            out.close();
            return true; //操作是否成功
        }catch (IOException e) {
            System.out.print("文件写入失败");
            return false;
        }
    }

    public static Map<String,String> getQQInfo(Context context ,InputStream is)throws IOException{
        byte[] buffer = new byte[is.available()];
        is.read(buffer);
        String json = new String(buffer,"utf-8");
        is.close();
        Gson gson = new Gson();
        Type type = new TypeToken< Map<String,String>>(){}.getType();
        Map<String,String>  userInfoMap = gson.fromJson(json,type);
        return userInfoMap;
    }
}
